In the last video we imported our very first time series data and we converted the column daytime into
the daytime index of our data frame.
And we did this by passing the column labor to the pyramids past states and index call of the Read CSP
method.
And in this video we will have a look at the second more explicit alternative to do this and two we
need two steps.
So first of all we transform the data type of the column to date time and we can do this.
This is the direct pen us method to date time and the second.
We said the column is the index of our data frame this the asset index method and then in the second
part of this video we will have a closer look to the to date time method and we plan in more detail
how to transform different strings into daytime objects.
So let's import pandas here and we import our temperate CSP file and TV for the first five rows and
we also quality of the Inform method.
So here we have now the column day time and we offer the data type object.
So these are strings here and we can also explicitly select the date time column.
So let's do this
and we can see at the end of that year we have the data type object and we can also select a single
element of our panda series or column slow.
Here we can see that this is actually a string and now we can convert the data type of our column to
date time with the direct pen us method to date time.
So here we pass our column date time
and also you will get a pen that serious with all of our dates here.
And if you have a look here at the very end we can see here that we have to date that type of daytime
60 for service of the method daytime.
We can transform strings into a daytime objects and now we're going to want to have the daytime column
here as being the daytime index of our whole data frame and we can do this in two steps.
So first of all we convert the data type with the method to data time and by doing so we're getting
you actually hear the pandas series and then we pass here the pandas serious to the method the set index.
So let's try this out here so we can see them now on the left hand side that instead of having a range
index we have our column date time and still we have the column day time so we can drop the column here
and now this looks quite good.
And uh then we can override our variable temperature so let's do this
and that's again never look at the first five rows so here we have our new data frame we have on the
left hand side our daytime index and we have the two columns L.A. and New York.
And let's also inspect the info method so we can see now our daytime indexes are worth thirty five thousand
entries and our two columns and we can also further inspect our daytime index.
But the attribute index.
So here we have the daytime index with all of the time stems and we can also select a single element
here.
So the first one and here we have a timestamp and actually panned US is quite smart and versatile when
it comes to converting strings to daytime optics.
So actually panels us can handle many different formats.
And uh the most send out format is actually a 215 or 520.
So we have here the 20th of May 2015 and we have here the format year.
Then we have a minus or dash.
Then we have months then and then minus or dash and then we have the day.
And this is no problem to convert into a daytime object.
So here we get the timestamp.
So you know our string we have the information about the date but uh we haven't passed any information
on the time.
And by default US assumes actually at midnight.
So we have uh Midnight Zero clock actually.
So this is here for the hours the minutes and the seconds but within I was doing we can also pass information
on the time.
So let's do this for example 10:00 and then we get to your 10:00 and we can also provide information
on the minutes.
So let's assume 10 30.
And we can also provide additional information on these seconds.
So for example 20 seconds.
And as I said before I actually PANDAS and the method to date time is quite powerful and versatile because
it can actually recognize and convert many different formats.
So for example here we have a different format.
So we have between year month and day no separate at all.
But this still works.
And again here by default we have at midnight.
If we do not pass any information on the time and we can also use different separate us.
So for example here slashes
or we can use that byte spaces and that's only one thing that we have to keep in mind here.
So by default the sequence this year month and day.
And for example if you pass now a year day and month.
So our intention here is to get the data the 20th of May 2015.
But here we switched the day and month and here penned us gets the problem.
So here we get an error message and it says months must be between one and twelve.
So by default pen US expects here the sequence that year month and day and this is definitely a source
for errors but we can also prevent this by having for the month for example in May.
So this also works and we can also locate the May at the very left side of our string or on the right
hand side.
And actually it doesn't matter if we have here uppercase let us all lowercase letters or whatever.
So it works.
And instead of having 20 for the day become can also for your 20th with the T H.
So this also works.
So to summarize so there's hardly any combination here.
It does not work and we can also pass a sequence with dates to the to date time method.
So for example here we have a list and here above we passed a panda series.
Our column dates time
and now we simply pass a list with two dates.
And this also works here and whenever we pass more than one date we get here a date time index.
So here you have now a day time index with two time stamps and we can also pass a sequence of dates
where for at least one element.
Even pandas will not be able to convert this into daytime so for example elephant.
So this might be impossible to convert but we can try here and let's have a look what daytime does here
and by default we get here an error message so unknown string format elephant.
So we have senior buffer that pandas can handle many string formats.
But of course elephant is quite impossible to convert.
And by default we are getting an error message.
But we can change this.
So let's have a look here and here we have the parameter arrows.
And by default it's set to raise.
So let's have a look here at the documentation so if we have the argument raise then the invalid passing
that raise an exception.
So we're getting an error message but we can also possibly coerce and then the invalid passing will
be set as an 80 year.
So this stands for not a time.
So let's try this out and let's pass codes to the parameter arrows
and it's from the cell here and here we can see that we nevertheless get a daytime index with the two
time stamps and actually the element an 80 year which stands for not a time.
So also here and having some strings which cannot be converted to daytime then still the daytime method
is quite smart and versatile.
All right.
With this we are finished now with this video and I hope to see you also in the next one by.